function callsPerSecChart() {
	setTimeout(function() {
		$.ajax({
			url : "/cdrwebgui/result/callsPerSec",
			type : "POST",
			complete : callsPerSecChart,
			success : function(response) {
				callsPerSecGraph.count += 1;

				if (!callsPerSecGraph.graph) {
					callsPerSecGraph.graph = $.jqplot("callsPerSecDiv", [ [
							callsPerSecGraph.count, response.count ] ]);
				} else {
					if (callsPerSecGraph.graph.series[0].data.length === 10) {
						callsPerSecGraph.graph.series[0].data.splice(0, 1);
					}

					callsPerSecGraph.graph.series[0].data.push([
							callsPerSecGraph.count, response.count ]);
					callsPerSecGraph.graph.resetAxesScale();
					callsPerSecGraph.graph.redraw();
				}
			}
		});
	}, 1000);
}

function durationsPerSecChart(response) {
	setTimeout(function() {
		$.ajax({
			url : "/cdrwebgui/result/durationsPerSec",
			type : "POST",
			complete : durationsPerSecChart,
			success : function(response) {
				durationsPerSecGraph.count += 1;

				if (!durationsPerSecGraph.graph) {
					durationsPerSecGraph.graph = $.jqplot("durationsPerSecDiv", [ [
							durationsPerSecGraph.count, response.duration / 60 ] ]);
				} else {
					if (durationsPerSecGraph.graph.series[0].data.length === 10) {
						durationsPerSecGraph.graph.series[0].data.splice(0, 1);
					}

					durationsPerSecGraph.graph.series[0].data.push([
							durationsPerSecGraph.count, response.duration / 60 ]);
					durationsPerSecGraph.graph.resetAxesScale();
					durationsPerSecGraph.graph.redraw();
				}
			}
		});
	}, 1000);
}

function failCallsPerSecChart(response) {
	setTimeout(function() {
		$.ajax({
			url : "/cdrwebgui/result/failedCallsPerSec",
			type : "POST",
			complete : failCallsPerSecChart,
			success : function(response) {
				failCallsPerSecGraph.count += 1;

				if (!failCallsPerSecGraph.graph) {
					failCallsPerSecGraph.graph = $.jqplot("failCallsPerSecDiv", [ [
							failCallsPerSecGraph.count, response.duration ] ]);
				} else {
					if (failCallsPerSecGraph.graph.series[0].data.length === 10) {
						failCallsPerSecGraph.graph.series[0].data.splice(0, 1);
					}

					failCallsPerSecGraph.graph.series[0].data.push([
							failCallsPerSecGraph.count, response.count ]);
					failCallsPerSecGraph.graph.resetAxesScale();
					failCallsPerSecGraph.graph.redraw();
				}
			}
		});
	}, 1000);
}

function getRuleResult() {
	setTimeout(function() {
		$.ajax({
			url : "/cdrwebgui/result/rule",
			type : "POST",
			complete : getRuleResult,
			success : function(response) {
				$.each(response, function(index, item) {
					if (item.rule.type === "DiscountForFailureRule") {
						var found = false;
						$.each(localResult.DiscountForFailureRule, function(index, rule) {
							if (rule.ruleId === item.rule.id) {
								found = true;
								if (rule.areas.length === 20) {
									rule.areas.splice(0, 1);
								}
								rule.areas.push(item.areaCode);
							}
						});
						
						if (!found) {
							localResult.DiscountForFailureRule.push({ruleId : item.rule.id, numberOfCalls: item.rule.numberOfCalls, areas: [item.areaCode]});
						}
					} else if (item.rule.type === "DiscountForManyCallsRule") {
						var found = false;
						$.each(localResult.DiscountForManyCallsRule, function(index, rule) {
							if (rule.ruleId === item.rule.id) {
								found = true;
								if (rule.phoneNumbers.length === 20) {
									rule.phoneNumbers.splice(0, 1);
								}								
								rule.phoneNumbers.push(item.phoneNumber);
							}
						});
						
						if (!found) {
							localResult.DiscountForManyCallsRule.push({ruleId : item.rule.id, numberOfCalls: item.rule.numberOfCalls, period: item.rule.period, phoneNumbers: [item.phoneNumber]});
						}
					} else if (item.rule.type === "DiscountForPopularUsersRule") {
						var found = false;
						$.each(localResult.DiscountForPopularUsersRule, function(index, rule) {
							if (rule.ruleId === item.rule.id) {
								found = true;
								if (rule.phoneNumbers.length === 20) {
									rule.phoneNumbers.splice(0, 1);
								}
								rule.phoneNumbers.push(item.phoneNumber);
							}
						});
						
						if (!found) {
							localResult.DiscountForPopularUsersRule.push({ruleId : item.rule.id, timeAmount: item.rule.timeAmount, timeUnit: item.rule.timeUnit, phoneNumbers: [item.phoneNumber]});
						}
					}
				});
				
				$("#discountForFailureRuleDiv").empty();
				$.each(localResult.DiscountForFailureRule, function(index, item) {
					$("<p></p>").html("If area get at least " + item.numberOfCalls + " unsuccessful calls, give discount").appendTo($("#discountForFailureRuleDiv"));
					
					var areaList = $("<ul></ul>");
					$.each(item.areas, function(areaIndex, area) {
						$("<li></li>").html(area).appendTo(areaList);
					});
					areaList.appendTo($("#discountForFailureRuleDiv"));
				});
				
				$("#discountForManyCallsRuleDiv").empty();
				$.each(localResult.DiscountForManyCallsRule, function(index, item) {
					$("<p></p>").html("If customer has at least " + item.numberOfCalls + " successful calls at " + item.period + ", give discount").appendTo($("#discountForManyCallsRuleDiv"));
					
					var phoneList = $("<ul></ul>");
					$.each(item.phoneNumbers, function(phoneIndex, phone) {
						$("<li></li>").html(phone).appendTo(phoneList);
					});
					phoneList.appendTo($("#discountForManyCallsRuleDiv"));
				});
				
				$("#discountForPopularUsersRuleDiv").empty();
				$.each(localResult.DiscountForPopularUsersRule, function(index, item) {
					$("<p></p>").html("If customer has at least " + item.timeAmount + item.timeUnit + " of call duration, give discount").appendTo($("#discountForPopularUsersRuleDiv"));
					
					var phoneList = $("<ul></ul>");
					$.each(item.phoneNumbers, function(phoneIndex, phone) {
						$("<li></li>").html(phone).appendTo(phoneList);
					});
					phoneList.appendTo($("#discountForPopularUsersRuleDiv"));
				});
			}
		});
	}, 5000);
}